﻿using Pegasus.Diagnostics;
using System;
using System.Windows.Forms;

namespace Pegasus.Globalization
{
	/// <summary>
	/// 
	/// </summary>
	internal class ResourceForms
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="res"></param>
		/// <param name="form"></param>
		internal static void ApplyResoucesToForms( ResourceManager res, Form form )
		{
			// Check params
			ParamCode.AssertNotNull( res, "res" );
			ParamCode.AssertNotNull( form, "form" );

			form.Text = ChangeString( res, form.Text );
			ProcessControls( res, form.Controls );

			if( form.Menu != null )
			{
				ProcessMenuItems( res, form.Menu.MenuItems );
			}

			if( form.ContextMenu != null )
			{
				ProcessMenuItems( res, form.ContextMenu.MenuItems );
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="res"></param>
		/// <param name="controls"></param>
		private static void ProcessControls( ResourceManager res, Control.ControlCollection controls )
		{
			// Check params
			ParamCode.AssertNotNull( res, "res" );
			ParamCode.AssertNotNull( controls, "controls" );

			foreach( Control ctrl in controls )
			{
				ProcessControl( res, ctrl );
				ProcessControls( res, ctrl.Controls );
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="res"></param>
		/// <param name="ctrl"></param>
		private static void ProcessControl( ResourceManager res, Control ctrl )
		{
			// Check params
			ParamCode.AssertNotNull( res, "res" );
			ParamCode.AssertNotNull( ctrl, "ctrl" );

			// Set the text of the control
			ctrl.Text = ChangeString( res, ctrl.Text );

			// Apply special processing for different types of controls
//			string type = ctrl.GetType().Name;
//			switch( type )
//			{
//				default:
//					break;
//			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="res"></param>
		/// <param name="menuItems"></param>
		private static void ProcessMenuItems( ResourceManager res, Menu.MenuItemCollection menuItems )
		{
			// Check params
			ParamCode.AssertNotNull( res, "res" );
			ParamCode.AssertNotNull( menuItems, "menuItems" );

			foreach( MenuItem menuItem in menuItems )
			{
				// Set the text of the menu item
				menuItem.Text = ChangeString( res, menuItem.Text );

				ProcessMenuItems( res, menuItem.MenuItems );
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="res"></param>
		/// <param name="text"></param>
		/// <returns></returns>
		private static string ChangeString( ResourceManager res, string text )
		{
			// Check params
			ParamCode.AssertNotNull( res, "res" );
			ParamCode.AssertNotNull( text, "text" );

			if( IsResourceName( text ) )
			{
				text = res[ GetResourceName( text ) ];
			}

			return text;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		private static bool IsResourceName( string name )
		{
			// Check params
			// name: may or may not have a value

			if( name != null && name.Length > 0 && 
				name[ 0 ] == '[' && name[ name.Length - 1 ] == ']' )
			{
				return true;
			}

			return false;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		private static string GetResourceName( string name )
		{
			// Check params
			ParamCode.AssertNotEmpty( name, "name" );
			return name.Trim( '[', ']' );
		}
	}
}
